<template>
  <div class="view-container zTable" style="padding-top:100px">
    <div class="app-title-container title-position">
      <modules-title titleName="字典管理"
        ><template slot="btn">
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="primary"
                icon="el-icon-plus"
                size="small"
                @click="handleAdd"
                v-hasPermi="{v:['sys:dict:add']}"
                >新增
              </el-button>
            </el-col>

            <!--   <el-col :span="1.5">
              <el-button
                type="danger"
                icon="el-icon-delete"
                :disabled="multiple"
                size="small"
                @click="handleDelete"
                >删除
              </el-button>
            </el-col> -->
          </el-row>
        </template>
      </modules-title>
    </div>
    <div class="main-content-container">
      <div class="app-main-container">
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          v-show="showSearch"
        >
          <el-form-item label="字典名称">
            <el-input
              @keyup.enter.native="handleQuery"
              clearable
              v-model="queryParams.dict_key"
              placeholder="请输入字典名称"
              size="small"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="字典类型" style="margin-left:40px">
            <el-select
              v-model="queryParams.dict_type"
              clearable
              @change="handleQuery"
              placeholder="请选择字典类型"
              size="small"
            >
              <el-option
                v-for="(fr,index) in dictionaryType"
                :key="index"
                :label="fr"
                :value="fr"
              />
            </el-select>
            <!-- <el-input
              clearable
              @keyup.enter.native="handleQuery"
              v-model="queryParams.dict_type"
              placeholder="请输入字典类型"
              size="small"
            ></el-input> -->
          </el-form-item>
          <el-form-item label="状态" style="margin-left:40px">
            <el-select
              v-model="queryParams.status"
              @change="selectChange"
              placeholder="请选择状态"
              size="small"
            >
              <el-option
                v-for="fr in projectFromOption"
                :key="fr.label"
                :label="fr.label"
                :value="fr.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item style="margin-left:40px">
            <el-button
              type="primary"
              icon="el-icon-search"
              size="small"
              @click="handleQuery"
              >搜索
            </el-button>
            <el-button
              plain
              icon="el-icon-refresh"
              size="small"
              @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
        <div class="zTable">
         <el-table
          ref="multipleTable"
          @selection-change="handleSelectionChange"
          v-loading="loading"
          border
          size="medium"
          :data="tableList"
          :default-sort="{ order: 'order_by' }"
        >
          <!-- <el-table-column type="selection" width="50"></el-table-column> -->
          <el-table-column
            label="字典名称"
            align="center"
            prop="dict_key"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="字典值"
            align="center"
            prop="dict_val"
            :show-overflow-tooltip='true'
          />
          <el-table-column
            label="字典类型"
            align="center"
            prop="dict_type"
            width=""
          ></el-table-column>

          <el-table-column
            label="备注"
            align="center"
            prop="remark"
          >
          </el-table-column>

          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="medium"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="{v:['sys:dict:edit']}"
              >
                修改
              </el-button>

              <el-button
                class="text-btn-red"
                size="medium"
                type="text"
                icon="el-icon-delete"
                v-hasPermi="{v:['sys:dict:delete']}"
                @click="handleDelete(scope.row)"
              >
                删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          style="display:flex;justify-content:flex-end;"
          :total="total"
          :page.sync="pages.pageNum"
          :limit.sync="pages.pageSize"
          @pagination="afreshList"
        />
        </div>
        
      </div>
    </div>
    <el-dialog
      :close-on-click-modal="false"
      class="dialog"
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="500px"
      :before-close="dialogVisibleClose"
    >
      <el-form
        :model="evaluateForm"
        :rules="rules"
        ref="ruleForm"
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-form-item label="字典名称" prop="dict_key"
          ><el-input v-model="evaluateForm.dict_key" placeholder="请输入字典名称"></el-input
        ></el-form-item>
        <el-form-item label="字典值" prop="dict_val"
          ><el-input v-model="evaluateForm.dict_val" placeholder="请输入字典值"></el-input
        ></el-form-item>
        <el-form-item label="字典类型" prop="dict_type">
          <el-autocomplete
                v-model="evaluateForm.dict_type"
                placeholder="请选择字典类型(可输入)" 
                :fetch-suggestions="querySearch"
                clearable
                @clear="setBlur()"
                @select="(item)=>{handleSelect(item)}"
                style="width:100%">
                <template slot-scope="{ item }">
                  <div class="product-item">
                    {{item}}
                  </div>
                </template>
              </el-autocomplete>
          <!-- <el-select
              style="width:100%"
              v-model="evaluateForm.dict_type"
              filterable
              clearable
              placeholder="请选择字典类型"
              size="small"
              @blur="selectType"
            >
              <el-option
                v-for="(fr,index) in dictionaryType"
                :key="index"
                :label="fr"
                :value="fr"
              />
            </el-select> -->
         </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio v-model="evaluateForm.status" :label="1">正常</el-radio>
          <el-radio v-model="evaluateForm.status" :label="-1">停用</el-radio>
        </el-form-item>
        <el-form-item label="排序" prop="order_by">
          <el-input
            placeholder="请输入排序字段，且排序字段为数字"
            v-model.number="evaluateForm.order_by"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            type="textarea"
            :class="textarea"
            placeholder="请输入内容"
            v-model="evaluateForm.remark"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleClose">取 消</el-button>
        <el-button type="primary" @click="dialogVisibleConfirm"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getDictionary,
  createDiction,
  removeDicction,
  deleteDicction,
  getDictionaryType
} from "@/api/authorization";
export default {
  name: "dictionaryManagement",

  data() {
    return {
      dictionaryType:[],
      textarea: "",
      // 查询参数
      queryParams: {
        status: 0,
        dict_type: "",
        dict_key: "",
        pager: {
          limit: 10,
          skip: 1,
          order_by: "",
          OrderMethod: 1
        }
      },
      // 表单参数
      editEvaluateForm: {
        dict_key: "",
        dict_val: "",
        status: 1,
        dict_type: null,
        order_by: null,
        remark: ""
      },
      evaluateForm: {
        id: "0",
        dict_key: "",
        dict_val: "",
        status: 1,
        dict_type: null,
        order_by: null,
        remark: ""
      },
      multipleSelection: [],
      multiple: false,
      single: false,
      dialogVisible: false,
      dialogTitle: "新增",

      // 遮罩层
      loading: true,
      showSearch: true,
      // 总条数
      total: 0,
      // 流程审批测试表格数据
      tableList: [],
      projectFromOption: [
        {
          label: "全部",
          value: 0
        },
        {
          label: "正常",
          value: 1
        },
        {
          label: "停用",
          value: -1
        }
      ],
      pages: {
        pageNum: 1,
        pageSize: 10
      },

      // 表单校验
      rules: {
        dict_key: [
          { required: true, message: "请输入字典名称", trigger: "blur" }
        ],
        dict_val: [
          { required: true, message: "请输入字典值", trigger: "blur" }
        ],
        dict_type: [
          { required: true, message: "请选择字典类型（可输入）", trigger:['blur','change']}
        ],
        order_by: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (/^(?:[1-9]\d*|0)$/.test(value) == false) {
                callback(new Error("请输入排序字段,且为正整数"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
          // {
          //   required: true,
          //   message: "请输入排序字段,且为数字类型",
          //   trigger: "blur",
          //   type: "number"
          // }
        ]
      },
      // 弹框
      evaluateRules: {
        result: [
          { required: true, message: "请选择评估结论", trigger: "blur" }
        ],
        remark: []
      }
    };
  },
  watch: {
    dialogVisible() {
      if (this.dialogVisible == false) {
        this.evaluateForm = {
          id: "0",
          dict_key: "",
          dict_val: "",
          status: 1,
          dict_type: null,
          order_by: null,
          remark: ""
        };
      }
    }
  },
  created() {
    this.getDictionaryData();
    this.getDictionaryTypeList()
  },
  methods: {
    setBlur() {
      document.activeElement.blur()
    },
    async querySearch(queryString, cb) {
      let res = await getDictionaryType();
      if (res.code === 200) {
        this.dictionaryType = res.data
      }
      var results = this.dictionaryType;
      cb(results);
    },
    handleSelect(item) {
      this.evaluateForm.dict_type = item
    },
    selectType(e){
      this.evaluateForm.dict_type = e.target.value
    },
    //获取字典列表
    getDictionaryTypeList(){
      getDictionaryType().then(res=>{
        console.log(res.data)
        if(res.code==200){
          this.dictionaryType = res.data
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    dialogVisibleConfirm() {
      this.$refs["ruleForm"].validate(val => {
        if (val) {
          if (this.dialogTitle == "新增") {
            let evaluateForm = {
              dict_key: this.evaluateForm.dict_key,
              dict_val: this.evaluateForm.dict_val,
              status: this.evaluateForm.status,
              dict_type: this.evaluateForm.dict_type,
              order_by: this.evaluateForm.order_by,
              remark: this.evaluateForm.remark
            }
            createDiction(evaluateForm).then(res => {
              if (res.code == 200) {
                this.getDictionaryData(this.queryParams);
                this.reset()
                this.getDictionaryTypeList()
              }else{
                this.$message.error(res.msg)
              }
            });
          } else {
            removeDicction(this.evaluateForm).then(res => {
              if (res.code == 200) {
                this.getDictionaryData(this.queryParams);
                this.reset()
                this.getDictionaryTypeList()
              }else{
                this.$message.error(res.msg)
              }
            });
          }
          this.dialogVisible = false;
        } else {
          return;
        }
      });
    },
    dialogVisibleClose() {
      this.dialogVisible = false;
      this.reset()
    },
    //新增字典
    handleAdd() {
      this.dialogTitle = "新增";
      this.dialogVisible = true;
    },
    selectChange() {
      this.getDictionaryData(this.queryParams);
    },
    afreshList() {
      /* this.queryParams.pager.limit = this.pages.pageSize;
      this.queryParams.pager.skip =
        (this.pages.pageNum - 1) * this.pages.pageSize; */
      this.getDictionaryData(this.queryParams);
    },
    /** 查询流程审批测试列表 */
    getDictionaryData() {
      console.log(this.pages.pageNum, this.pages.pageSize);

      let data = {
        condition: {
          /* status: this.queryParams.status, */
          status: this.queryParams.status,
          dict_type: this.queryParams.dict_type,
          dict_key: this.queryParams.dict_key
        },
        pager: {
          limit: this.pages.pageSize,
          skip: this.pages.pageNum
        }
      };
      getDictionary(data).then(res => {
        if (res.code == 200) {
          this.tableList = [];
          this.tableList = res.data.data;
          this.total = res.data.total;
        }
        this.loading = false;
        console.log(this.tableList);
      });
    },

    // 表单重置
    reset() {
      // this.queryParams = {
      //   status: 0,
      //   dict_type: "",
      //   dict_key: ""
      // };
      this.evaluateForm = {
          id: "0",
          dict_key: "",
          dict_val: "",
          status: 1,
          dict_type: null,
          order_by: null,
          remark: ""
        };
        this.resetForm("ruleForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pages={
        pageNum: 1,
        pageSize: 10
      },
      this.getDictionaryData();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        status: 0,
        dict_type: "",
        dict_key: ""
      };
      this.getDictionaryData();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.dialogTitle = "修改";
      console.log(row);
      this.evaluateForm = {
        id: row.id,
        dict_key: row.dict_key,
        dict_val: row.dict_val,
        status: row.status,
        dict_type: row.dict_type,
        order_by: row.order_by,
        remark: row.remark
      },
      this.dialogVisible = true;
    },

    // 创建立项申请
    createProject(row) {
      this.$router.push({
        name: "createEdit",
        query: {
          types: 1
        }
      });
    },
    // 草稿提交审核
    handleSubmitAudit(row) {},
    /** 删除按钮操作 */
    async handleDelete(row) {
      const ids = row.id || this.ids;
      await this.$confirm("是否确认删除字典 " + row.dict_key + " ?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          console.log(this.evaluateForm.id),
            console.log(this.multipleSelection),
            deleteDicction([row.id]).then(res => {
              if(res.code==200){
                if (this.tableList.length == 1 && this.pages.pageNum > 1) {
                  this.pages.pageNum--;
                }
                this.afreshList(); 
              }else{
                this.$message.error(res.msg)
              }
              
            });
        })
        .catch(function() {});
    }
  } //methods结束
};
</script>
<style lang="scss" scoped>
::v-deep .el-button--text {
  padding: 0;
  }
.textarea {
  min-height: 31px;
  margin-top: 0px;
  margin-bottom: 0px;
  height: 31px;
}

</style>
<style>
  .el-tooltip__popper{
    max-width:20%;
  }
.zTable .el-tooltip__popper,.el-tooltip__popper.is-dark{
    background:rgb(48, 65, 86) !important;
    color: #fff !important;
    line-height: 24px;
  }
</style>

